import 'package:flutter/cupertino.dart' show CupertinoIcons;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jm_flutter/jm_flutter.dart';

class MyScaffold extends StatelessWidget {
  const MyScaffold({
    Key? key,
    this.title,
    this.centerTitle = true,
    this.hideAppBar = false,
    this.appBarColor,
    this.appBarTextColor,
    this.backgroundColor,
    required this.body,
    this.appBarHeight,
    this.backOnTap,
    this.closeBack = false,
    this.hideDivider = true,
    this.actions,
    this.scroll = false,
    this.floatingActionButton,
    this.i18n = false,
    this.statusBarType,
    this.leading,
  }) : super(key: key);

  final String? title;
  final bool centerTitle;
  final bool hideAppBar;
  final bool hideDivider;
  final bool closeBack;
  final bool scroll;
  final Color? appBarColor;
  final Color? appBarTextColor;
  final Color? backgroundColor;
  final Widget body;
  final double? appBarHeight;

  final VoidCallback? backOnTap;

  final Widget? leading;
  final List<Widget>? actions;
  final Widget? floatingActionButton;
  final bool i18n;
  final StatusBarType? statusBarType;

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: JmStatusBar(type: statusBarType),
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: appBarColor ?? JmColors.appBarBg,
        body: SafeArea(
            bottom: false,
            child: Column(
              children: <Widget>[
                hideAppBar
                    ? Container()
                    : Container(
                        alignment: Alignment.center,
                        height: appBarHeight ?? MyScreen.appBarHeight,
                        //color: appBarColor ?? JmColors.main,
                        padding: const EdgeInsets.only(left: 4, right: 4),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                              flex: 2,
                              child: Container(
                                alignment: Alignment.centerLeft,
                                child: Visibility(
                                  visible: !closeBack,
                                  child: leading ??
                                      IconButton(
                                        iconSize: 28,
                                        icon: Icon(
                                          CupertinoIcons.back,
                                          //size: 26,
                                          color: appBarTextColor ??
                                              JmConfig.appBarTextStyle.color,
                                        ),
                                        onPressed: () {
                                          if (backOnTap != null) {
                                            backOnTap!();
                                          } else {
                                            //Get.back();
                                            Navigator.pop(context);
                                          }
                                        },
                                      ),
                                ),
                              ),
                            ),
                            Expanded(
                              flex: 3,
                              child: title != null
                                  ? Container(
                                      alignment: Alignment.center,
                                      child: MyText(
                                        title!,
                                        i18n: i18n,
                                        size: JmConfig.appBarTextStyle.fontSize,
                                        color: appBarTextColor ??
                                            JmConfig.appBarTextStyle.color,
                                        overflow: TextOverflow.ellipsis,
                                        weight:
                                            JmConfig.appBarTextStyle.fontWeight,
                                        maxLines: 1,
                                      ),
                                    )
                                  : Container(),
                            ),
                            Expanded(
                                flex: 2,
                                child: actions != null && actions!.isNotEmpty
                                    ? Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.end,
                                        children: actions!,
                                      )
                                    : Container()),
                          ],
                        ),
                      ),
                Visibility(
                  visible: !hideDivider,
                  child: const Divider(
                    height: 0.8,
                  ),
                ),
                Expanded(
                  child: scroll
                      ? Container(
                          width: double.infinity,
                          color: backgroundColor ?? JmColors.bg,
                          child: SingleChildScrollView(
                            child: body,
                            physics: const BouncingScrollPhysics(),
                          ),
                        )
                      : Container(
                          color: backgroundColor ?? JmColors.bg,
                          width: double.infinity,
                          child: body,
                        ),
                )
              ],
            )),
        floatingActionButton: floatingActionButton,
      ),
    );
  }
}
